
//static const uint8_t SS      = 8;  /* P2.7 */
//static const uint8_t SCK     = 7;  /* P3.2 */
//static const uint8_t MOSI    = 15;  /* P3.0 */
//static const uint8_t MISO    = 14;  /* P3.1 */

//注意在这个库中，SS其实没有被实现，所以需要自己拉高或拉低相关的数据
#include <SPI.h>

#define READ_JEDEC_ID 0x9F
#define READ_STATUS_1 0x05
#define READ_DATA 0x03
#define WRITE_ENABLE 0x06
#define PAGE_PROGRAM 0x02
#define CHIP_ERASE 0xC7

byte pageBuffer[256];
char str[] = "An apple a day keeps the doctor away."; //short than 256

//#define SS P2_7
void setup()
{
    pinMode(SS,OUTPUT);
    SPI.begin();
    SPI.setDataMode(SPI_MODE0);
    SPI.setBitOrder(MSBFIRST);
    Serial.begin(9600);

    uint32_t a=0x123456;

    Serial.println((uint8_t)(a>>16),HEX);
    Serial.println((uint8_t)(a>>8),HEX);
    Serial.println((uint8_t)(a&0xFF),HEX);


    //ReadID();
    //EraseChip();
    //WritePage(0x123456, str, sizeof(str));
}

void loop()
{

	//WritePage(0x000001, str, sizeof(str));
	//delay(1000);

	uint32_t pageNumber=0x000001;
    ReadPage(pageNumber, pageBuffer,sizeof(str));
    for(int i=0; i < sizeof(str); i++)
    {
        Serial.print(char(pageBuffer[i]));
    }
    Serial.println();


    delay(2000);


}

void CheckBusy()
{
    digitalWrite(SS, HIGH);
    digitalWrite(SS, LOW);
    SPI.transfer(READ_STATUS_1);
    while(SPI.transfer(0) & 0x01);
    digitalWrite(SS, HIGH);
}

void ReadID()
{
    digitalWrite(SS, HIGH);
    digitalWrite(SS, LOW);
    SPI.transfer(READ_JEDEC_ID);
    byte manuID = SPI.transfer(0);
    byte memoType = SPI.transfer(0);
    byte capa = SPI.transfer(0);
    digitalWrite(SS, HIGH);

    Serial.print("Manufacturer ID: "); Serial.println(manuID, HEX);
    Serial.print("Memory Type: "); Serial.println(memoType, HEX);
    Serial.print("Capacity : "); Serial.println(capa, HEX);

    CheckBusy();
}

void ReadPage(uint32_t pageNumber, byte pageBuffer[], int length)
{
    // pageNumber: 16-bit data
    digitalWrite(SS, HIGH);
    digitalWrite(SS, LOW);
    SPI.transfer(READ_DATA);
    SPI.transfer((uint8_t)(pageNumber>>16));
    SPI.transfer((uint8_t)(pageNumber>>8));
    SPI.transfer((uint8_t)(pageNumber>>8));
    //SPI.transfer(0);
    for(int i = 0; i < length; i++)
    {
        pageBuffer[i] = SPI.transfer(0);
    }
    digitalWrite(SS, HIGH);
    CheckBusy();
}

void WritePage(uint32_t pageNumber, char pageBuffer[], int length)
{
    digitalWrite(SS, HIGH);
    digitalWrite(SS, LOW);
    SPI.transfer(WRITE_ENABLE);
    digitalWrite(SS, HIGH);
    digitalWrite(SS, LOW);
    SPI.transfer(PAGE_PROGRAM);
    SPI.transfer((uint8_t)(pageNumber>>16));
    SPI.transfer((uint8_t)(pageNumber>>8));
    SPI.transfer((uint8_t)(pageNumber>>8));
    //SPI.transfer(0);
    for(int i = 0; i < length; i++)
    {
        SPI.transfer(byte(pageBuffer[i]));
    }
    digitalWrite(SS, HIGH);
    CheckBusy();
}

void EraseChip()
{
    digitalWrite(SS, HIGH);
    digitalWrite(SS, LOW);
    SPI.transfer(WRITE_ENABLE);
    digitalWrite(SS, HIGH);
    digitalWrite(SS, LOW);
    SPI.transfer(CHIP_ERASE);
    digitalWrite(SS, HIGH);
    CheckBusy();
    Serial.println("Erase finished");
}
